package com.reportgenerate.marketreport.service.implement;

import com.reportgenerate.marketreport.entity.model.Customer;
import com.reportgenerate.marketreport.entity.model.CustomerExample;
import com.reportgenerate.marketreport.exception.DeleteObjectException;
import com.reportgenerate.marketreport.exception.SaveObjectException;
import com.reportgenerate.marketreport.mapper.CustomerMapper;
import com.reportgenerate.marketreport.service.CustomerService;
import com.reportgenerate.marketreport.service.RowBoundsToPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @program: reportgenerate
 * @Description: 客户服务的实现类
 * @author: Mr.Cheng
 * @Email: ccelephant_518@126.com
 * @date: 2023/8/20 20:11
 */
@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private RowBoundsToPage rowBoundsToPage;

    /**
     * @Description: 获取客户信息
     * @Param: []
     * @return: java.lang.String
     * @Author: Mr.Cheng
     * @Email: ccelephant_518@126.com
     * @Date: 2023/8/20 17:02
     */
    @Override
    public List<Customer> getCustomerInformationList() {
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andIdIsNotNull();
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        return customerList;
    }

    /**
     * @Description: 获取客户信息分页
     * @Param: []
     * @return: java.lang.String
     * @Author: Mr.Cheng
     * @Email: ccelephant_518@126.com
     * @Date: 2023/8/20 17:04
     */
    @Override
    public HashMap getCustomerInformationListByPage(CustomerExample customerExample, Integer page, Integer size) {
        RowBounds rowBounds = new RowBounds(rowBoundsToPage.offset(page,size), rowBoundsToPage.limit(size));
        List<Customer> customerList = customerMapper.selectByExampleWithRowbounds(customerExample, rowBounds);
        HashMap<String, Object> customerMap = new HashMap<>();
        customerMap.put("page", page);
        customerMap.put("size", size);
        customerMap.put("customerlist", customerList);
        return customerMap;
    }

    /**
     * @param customer
     * @Description: 插入客户信息
     * @Param: [customer]
     * @return: int
     * @Author: Mr.Cheng
     * @Email: ccelephant_518@126.com
     * @Date: 2023/8/20 20:06
     */
    @Override
    @Transactional(rollbackFor = SaveObjectException.class)
    public int insertCustomerInformation(Customer customer) {
        int insertResult = 0;
        try {
            insertResult = customerMapper.insertSelective(customer);
        } catch (Exception e) {
            throw new SaveObjectException(e.getMessage(),customer.getFirstName());
        }
        return insertResult;
    }

    /**
     * @param id
     * @Description: 删除客户信息
     * @Param: [id]
     * @return: int
     * @Author: Mr.Cheng
     * @Email: ccelephant_518@126.com
     * @Date: 2023/8/20 20:08
     */
    @Override
    @Transactional(rollbackFor = DeleteObjectException.class)
    public int deleteCustomerInformationById(String id) {
        int deleteResult = 0;
        try {
            deleteResult = customerMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            throw new DeleteObjectException(e.getMessage(),id);
        }
        return deleteResult;
    }

    /**
     * @param id
     * @Description: 根据客户id获取客户信息
     * @Param: [id]
     * @return: com.reportgenerate.marketreport.entity.model.Customer
     * @Author: Mr.Cheng
     * @Email: ccelephant_518@126.com
     * @Date: 2023/8/20 20:11
     */
    @Override
    public Customer getCustomerInformationById(String id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    /**
    * @Description: 更新客户信息
    * @Param: [customer]
    * @return: int
    * @Author: Mr.Cheng
    * @Email: ccelephant_518@126.com
    * @Date: 2023/8/21 21:34
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCustomerInformation(Customer customer) {
        return 0;
    }
}
